package com.lcq.springbootcreate.project.controller;

import com.github.pagehelper.PageInfo;
import com.lcq.springbootcreate.project.ExceptionAndEnum.MallExceptionEnum;
import com.lcq.springbootcreate.project.common.ApiRestResponse;
import com.lcq.springbootcreate.project.common.Constant;
import com.lcq.springbootcreate.project.entity.User;
import com.lcq.springbootcreate.project.request.AddCategoryReq;
import com.lcq.springbootcreate.project.request.UpdateCategoryReq;
import com.lcq.springbootcreate.project.service.CategroryService;
import com.lcq.springbootcreate.project.service.UserService;
import com.lcq.springbootcreate.project.vo.CategoryVO;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 描述：商品分类模块
 */
@RestController
public class CategoryController {
    @Autowired
    CategroryService categroryService;

    @Autowired
    UserService userService;


    // 参数校验的注解 @Valid 表示参数需要校验
    @PostMapping("/admin/category/add")
    @Validated
    @ApiOperation("商品模块添加商品")
    public ApiRestResponse addCategory(HttpSession session, @RequestBody AddCategoryReq addCategoryReq) {

        User user = (User) session.getAttribute(Constant.MALL_USER);
        if (user == null) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        boolean isAdmin = userService.checkAdminRole(user);
        if (isAdmin) {
            System.out.println("是管理员开始添加商品");
            categroryService.addCategory(addCategoryReq);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NOT_ADIMIN);
        }

        return ApiRestResponse.success(MallExceptionEnum.CATEGORY_ADD_SUCCESS);
    }

    @PostMapping("/admin/category/update")
    @Validated
    @ApiOperation("商品模块更新商品")
    public ApiRestResponse updateCategory(HttpSession session, @RequestBody UpdateCategoryReq updateCategoryReq) {

        User user = (User) session.getAttribute(Constant.MALL_USER);
        if (user == null) {
            return ApiRestResponse.error(MallExceptionEnum.NEED_LOGIN);
        }
        boolean isAdmin = userService.checkAdminRole(user);
        if (isAdmin) {
            System.out.println("是管理员开始更新商品");
            categroryService.updateCategory(updateCategoryReq);
        } else {
            return ApiRestResponse.error(MallExceptionEnum.NOT_ADIMIN);
        }

        return ApiRestResponse.success(MallExceptionEnum.CATEGORY_ADD_SUCCESS);
    }


    @GetMapping("/category/delete")
    @Validated
    @ApiOperation("商品模块更新商品")
    public ApiRestResponse deletCeategory(@RequestParam Integer id) {

        categroryService.deleteById(id);
        return ApiRestResponse.success();
    }

    @GetMapping("/category/adlist")
    @ApiOperation("分页展示")
    public ApiRestResponse listForAdmin(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        PageInfo pageInfo = categroryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @GetMapping("/category/list")
    @ApiOperation("分页展示")
    public ApiRestResponse listForCustomer(){
        List<CategoryVO> categoryVOList = categroryService.listForCustomer();
        return ApiRestResponse.success(categoryVOList);
    }



}
